import { create } from 'zustand';

interface Pick3DFocusState {
  focusId: string;
  allPickIds: string[];
  setFocused: (newConsole: string) => void;
  addPickId: (newId: string) => void;
  removePickId: (newId: string) => void;
  clear: () => void;
  focusFirst: () => void;
  reset: () => void;
  next: () => void;
  setSelectedEntityIds: (newIds) => void; // 将会弃用
  isNext: boolean; // 将会弃用
  resetCount: number; // 将会弃用
  selectedEntityIds: { [key: string]: string[] }; // 将会弃用
}

export const useFocusStore = create<Pick3DFocusState>((set) => ({
  isNext: false,
  resetCount: 0,
  selectedEntityIds: {},
  focusId: null,
  allPickIds: [],
  setSelectedEntityIds: (newIds) => {
    set({ selectedEntityIds: newIds });
  },
  setFocused: (newConsole) => {
    set({ focusId: newConsole });
  },
  next: () => {
    set((state) => {
      const next_index = state.allPickIds.indexOf(state.focusId) + 1;
      return { focusId: state.allPickIds[next_index] };
    });
  },
  addPickId: (newId) =>
    set((state) => {
      const temp = new Set(state.allPickIds);
      temp.add(newId);
      return { allPickIds: Array.from(temp) };
    }),
  removePickId: (newId) =>
    set((state) => {
      const temp = new Set(state.allPickIds);
      temp.delete(newId);
      return { allPickIds: Array.from(temp) };
    }),
  clear: () => set({ allPickIds: [], focusId: null }),
  reset: () => set((state) => ({ focusId: state.allPickIds[0] })),
  focusFirst: () => set((state) => ({ focusId: state.allPickIds[0] })),
}));
